<?php

if (!class_exists("PST_PRESENTATION")) {

    class PST_PRESENTATION {

        protected $array = array();
        protected $view = null;
        protected $html = null;
        protected $readonly = null;
        protected $label = null;
        protected $field = null;
        protected $name = null;
        protected $id = null;
        protected $value = "";
        protected $required = null;
        protected $model = null;
        protected $isvisible = true;
        protected $type = true;
        protected $initialvalue = null;
        protected $size = null;
        protected $table = null;
        public $classparams = null;
        public $js_initial = null;
        public $js_params = array();

        public function __construct($settings) {
            $this->generateObject($settings);

            // Si viene un valor por defecto lo define y lo setea en el POST
            if (isset($settings['initialvalue']) && empty($this->value)) {
                $this->value = $settings['initialvalue'];
                if (isset($this->field)) {
                    $field = explode(".", $this->field);
                    if (count($field) == 2) {
                        $_POST['data']['Cake'] = array(
                            '_filter' => '1',
                            '_output' => 'html'
                        );
                        $_POST['data'][$field[0]][$field[1]] = $settings['initialvalue'];
                    }
                }
            }

            // Si no es visible se setea como hidden
            if (!$this->isvisible) {
                $this->array['type'] = "hidden";
            }

            // Si tiene una nota la agrega
            if (!empty($this->note)) {
                $this->array['after'] = '<div class="note">' . $this->note . '</div>';
            }

            // Seteo el id del div que contiene al campo
            $this->array['div']['id'] = 'box' . $this->id;
        }

        protected function generateObject($settings) {
            foreach($settings as $key => $value) {
                $this->$key = $value;
            }
            
            if (empty($this->field)) {
                $this->field = $this->name;
            }
            
            if (!is_object($this->view)) {
                $this->view = null;
            }
            
            if (isset($this->required) && $this->required === true) {
                $this->required = "required";
            }
            else {
                $this->required = "";
            }
            
            if (isset($this->classparams)) {
                $this->classparams = json_decode($this->classparams, true);
            }
        }

        // Render para los filtros de los search and list
        public function renderFilterForm() {
            return $this->renderMaintForm();
        }

        // Render para el formulario del maint
        public function renderMaintForm() {
            $this->array['default'] = $this->value;
            if (!empty($this->size)) {
                $this->array['size'] = $this->size;
                $this->array['maxlength'] = $this->size;
                $this->array['style'] = "width: auto;";
            }
            if ($this->readonly) {
                $this->html = true;
                return $this->renderReadOnly();
            }
            return $this->array;
        }

        // Render para las tablas asociadas del maint
        public function renderTableMaint() {
            if (isset($this->isvisible) && !$this->isvisible) {
                return '<input type="hidden" name="' . $this->name . '" id="' . $this->id . '" value="' . $this->value . '" />';
            }

            if ($this->readonly) {
                $this->html = false;
                return $this->renderReadOnly();
            }

            $html = '<div id="box' . $this->id . '" class="input ' . $this->array['type'] . ' ' . $this->required . '">';
            $html.= '<label for="' . $this->id . '">' . $this->label . '</label>';
            $html.= '<input type="' . $this->array['type'] . '" name="' . $this->name . '" id="' . $this->id . '" ';
            if (!empty($this->size)) {
                $html.= 'size = ' . $this->size . ' maxlength = ' . $this->size . ' style = "width: auto;" ';
            }
            $html.= '/>';
            if (!empty($this->note)) {
                $html.= '<div class="note">' . $this->note . '</div>';
            }
            $html.= '</div>';
            return $html;
        }

        // Render para cuando sea readonly
        public function renderReadOnly() {
            if (isset($this->isvisible) && !$this->isvisible) {
                return '<input type="hidden" name="' . $this->name . '" id="' . $this->id . '" value="' . $this->value . '" />';
            }

            if ($this->html) {
                $html = '<div id="box' . $this->id . '" class="input ' . $this->array['type'] . ' ' . $this->required . '">';
                $html.= '<input type="hidden" name="' . $this->name . '" id="' . $this->id . '" value="' . $this->value . '" />';
                $html.= '<label for="' . $this->id . '">' . $this->label . '</label>';
                $html.= '<span class="field">' . $this->getHelperValue() . '</span>';
                $html.= '</div>';
                return $html;
            }

            return $this->getHelperValue();
        }

        // Devuelve el valor adaptado para mostrar
        protected function getHelperValue() {
            return $this->loadDbQuery();
        }

        // Incluye los archivo javascript necesarios para correr el presentation
        public function jsIncludes() {
            return array();
        }

        // Metodo para filtrar el registro en un WHERE de la query
        public function objectDBQuery() {
            if (isset($this->field) && isset($this->value)) {
                $type = strtolower(gettype($this->value));
                $field = $this->field;
                
                if ($type == "integer" || $type == "double" || $type == "null") {
                    return array($field => $this->value);
                } elseif ($type == "string") {
                    if (is_numeric($this->value)) {
                        return array($field => $this->value);
                    } else {
                        return array($field . " LIKE" => "%" . $this->value . "%");
                    }
                }
            }

            return array();
        }

        // Metodo para obtener el valor del campo una vez traido desde la BD
        public function loadDbQuery() {
            if (isset($this->value)) {
                return $this->value;
            }
            return "";
        }

        // Metodo para salvar el registro en un INSERT o UPDATE de la query
        public function saveDbQuery() {
            if (isset($this->value)) {
                return $this->value;
            }
            return "";
        }

    }

}
